package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Shader;


import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;

import java.util.List;

class AreaLayer extends BasicLayer
{
	public AreaLayer()
	{
		super();
	}

	@Override
	public Layer.ChartTypeEnum getLayerTypeEnum()
	{
		return Layer.ChartTypeEnum.AREA;
	}

	@Override
	public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		List<CandleData> tDataList = m_callback.getCandleDataList();
		if (tDataList != null && tDataList.size() > 0)
		{
			int tListSize = tDataList.size();
			Coordinate tCoordinate = m_callback.getCoordinate();
			int tStartIndex = tCoordinate.getStartIndex();
			float tXFirstPosition = tCoordinate.getXFirstPosition();
			float tXInterval = tCoordinate.getXInterval();
			float tYFirstposition = tCoordinate.getYFirstPosition();
			float tYMaxValue = tCoordinate.getMaxValue();
			float tYMinValue = tCoordinate.getMinValue();
			float tYValueDist = tYMaxValue - tYMinValue;
			float tYValueLength = tCoordinate.getYInterval() * tCoordinate.getYIntervalCount();
			RectF tChartRect = tCoordinate.getLayerRectF();

			float tCurrentYPosition = 0.f;
			float tTempYPosition = 0.f;
			float tCurrentXPosition = 0.f;
			float tTempXPosition = 0.f;

			Path tAreaPath = new Path();

			pPaint.setStrokeWidth(2);
			pPaint.setStyle(Paint.Style.FILL);
			pPaint.setAntiAlias(true);
			pPaint.setColor(m_callback.getSysBgColor());
			float tTopPosition = tCoordinate.getYFirstPosition();

			for (int i = tStartIndex; i < tListSize; i++)
			{
				float tVolumn = (float) tDataList.get(i).getDbClose();

				tTempXPosition = tCurrentXPosition;
				tCurrentXPosition = tXFirstPosition + tXInterval * (i - tStartIndex);
				tTempYPosition = tCurrentYPosition;
				tCurrentYPosition = tYFirstposition - ((tVolumn - tYMinValue) * tYValueLength / tYValueDist);

				tTopPosition = tTopPosition < tCurrentYPosition ? tTopPosition : tCurrentYPosition;

				if (i != tStartIndex)
				{
					pCanvas.drawLine(tTempXPosition, tTempYPosition, tCurrentXPosition, tCurrentYPosition, pPaint);
				}
				pCanvas.drawCircle(tCurrentXPosition, tCurrentYPosition, 3, pPaint);

				if (i == tStartIndex)
				{
					tAreaPath.moveTo(tXFirstPosition, tChartRect.bottom);
					tAreaPath.lineTo(tXFirstPosition, tCurrentYPosition);
				}
				tAreaPath.lineTo(tCurrentXPosition, tCurrentYPosition);
				if (i == (tListSize - 1))
				{
					tAreaPath.lineTo(tCurrentXPosition, tChartRect.bottom);
					tAreaPath.lineTo(tXFirstPosition, tChartRect.bottom);
				}
			}

			if (m_bGradient)
			{
				Shader mShader = new LinearGradient(0, tTopPosition, 0, tChartRect.bottom, new int[]
				{ Color.argb((Color.alpha(m_callback.getSysBgColor())) / 5, Color.red(m_callback.getSysBgColor()), Color.green(m_callback.getSysBgColor()), Color.blue(m_callback.getSysBgColor())), Color.argb((Color.alpha(m_callback.getSysBgColor())) / 10, Color.red(m_callback.getSysBgColor()), Color.green(m_callback.getSysBgColor()), Color.blue(m_callback.getSysBgColor())) }, null, Shader.TileMode.CLAMP);
				pPaint.setShader(mShader);
			}
			else
			{
				pPaint.setColor(Color.argb((Color.alpha(m_callback.getSysBgColor())) / 5, Color.red(m_callback.getSysBgColor()), Color.green(m_callback.getSysBgColor()), Color.blue(m_callback.getSysBgColor())));
			}

			pCanvas.drawPath(tAreaPath, pPaint);
			pPaint.reset();
			pPaint.setAntiAlias(true);
		}
	}
}
